package com.example.concurrentprogramming8016.thread;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * @author liyanhao
 * @Date 2023-01-03 16:54
 */
public class Piped {
    public static void main(String[] args) throws IOException {
        PipedWriter out = new PipedWriter();
        PipedReader in = new PipedReader();
        // 将输入流和输出流链接 否在在使用的时候会报错
        out.connect(in);
        Thread thread = new Thread(new Print(in), "PrintThread");
        thread.start();
        int receive = 0;
        while ((receive = System.in.read()) != -1) {
            out.write(receive);
        }
        out.close();

    }
}

 class Print implements Runnable {
    private PipedReader in;

     public Print(PipedReader in) {
         this.in = in;
     }

     @Override
    public void run() {
        int receive = 0;
         while (true) {
             try {
                 if ((receive = in.read()) != -1) {
                     System.out.println("打印输出：");
                     System.out.println((char) receive);
                 }

             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
         }
    }
}
